﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Linq.Expressions;

namespace ACP.Data
{
    public class Repository<T> : IRepository<T> where T : class
    {
        public IUnitOfWork UnitOfWork;
        private IDbSet<T> ObjectSet;
        //private IDbSet<T> ObjectSet
        //{
        //    get
        //    {
        //        if (_objectset == null)
        //        {
        //            _objectset = UnitOfWork.Context.Set<T>();
        //        }
        //        return _objectset;
        //    }
        //}

        public Repository()
        {
            if (this.UnitOfWork == null)
            {
                this.UnitOfWork = new UnitOfWork();
            }
            ObjectSet = UnitOfWork.Context.Set<T>();
        }

        public Repository(IUnitOfWork unitOfWork)
        {
            ObjectSet = unitOfWork.Context.Set<T>();
            //shareContext = true;
        }

        public IQueryable<T> AsQueryable()
        {
            return ObjectSet;
        }
        public IEnumerable<T> GetAll()
        {
            return ObjectSet;
        }

        public IEnumerable<T> GetAllOrderBy(Expression<Func<T, bool>> orderBy)
        {
            return ObjectSet.OrderBy(orderBy).ToList();
        }

        public IEnumerable<T> GetAllOrderByDescending(Expression<Func<T, bool>> orderByDescending)
        {
            return ObjectSet.OrderByDescending(orderByDescending).ToList();
        }

        public IEnumerable<T> Find(Expression<Func<T, bool>> where)
        {
            return ObjectSet.Where(where);
        }

        public IEnumerable<T> FindWithInclude(Expression<Func<T, bool>> where, string IncludeEntity)
        {
            return ObjectSet.Where(where).Include(IncludeEntity);
        }

        public IEnumerable<T> GetAllWithIncludes(string[] includes)
        {
            if (includes.Length == 1)
                return ObjectSet.Include(includes[0]).ToList();
            else if (includes.Length == 2)
                return ObjectSet.Include(includes[0]).Include(includes[1]).ToList();
            else if (includes.Length == 3)
                return ObjectSet.Include(includes[0]).Include(includes[1]).Include(includes[2]).ToList();
            else if (includes.Length == 4)
                return ObjectSet.Include(includes[0]).Include(includes[1]).Include(includes[2]).Include(includes[3]).ToList();
            else
                return ObjectSet.ToList();
        }

        public T Single(Expression<Func<T, bool>> where)
        {
            return ObjectSet.SingleOrDefault(where);
        }

        public T First(Expression<Func<T, bool>> where)
        {
            return ObjectSet.FirstOrDefault(where);
        }


        public void Delete(T entity)
        {
            ObjectSet.Remove(entity);
        }

        public void Add(T entity)
        {
            ObjectSet.Add(entity);
        }

        public void AddObjects(ICollection<T> lstEntities)
        {
            foreach (var item in lstEntities)
            {
                ObjectSet.Add(item);
            }
        }

        public void Update(T entity)
        {
            if (entity != null)
            {
                var entry = UnitOfWork.Context.Entry(entity);
                ObjectSet.Attach(entity);
                entry.State = System.Data.EntityState.Modified;
            }
        }

        //public virtual T Create(T TObject)
        //{
        //    var newEntry = _objectSet.Add(TObject);
        //    if (!shareContext)
        //        Context.SaveChanges();
        //    return newEntry;
        //}

        public virtual int Count
        {
            get
            {
                return ObjectSet.Count();
            }
        }

        //public virtual int DeleteObject(T TObject)
        //{
        //    _objectSet.Remove(TObject);
        //    if (!shareContext)
        //        return Context.SaveChanges();
        //    return 0;
        //}

        //public virtual int Update(T TObject)
        //{
        //    var entry = Context.Entry(TObject);
        //    ObjectSet.Attach(TObject);
        //    entry.State = EntityState.Modified;
        //    if (!shareContext)
        //        return Context.SaveChanges();
        //    return 0;
        //}

        //public void Dispose()
        //{
        //    //if (shareContext && (Context != null))
        //    //    Context.Dispose();
        //    if (shareContext && (Context != null))
        //        Context.Dispose();
        //    else if (Context != null)
        //        Context.Dispose();
        //}

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (UnitOfWork.Context != null)
                {
                    UnitOfWork.Context.Dispose();
                    UnitOfWork.Context = null;
                }
            }
        }

        //public void Dispose()
        //{
        //    if (Context != null)
        //    {
        //        Context.Dispose();
        //    }
        //    GC.SuppressFinalize(this);
        //}

        //public void Commit()
        //{
        //    Context.SaveChanges();
        //}

    }
}
